import App from "./App.vue";
import router from "./router";
import { createPinia } from "pinia";

import * as ElIcon from "@element-plus/icons-vue";
import "element-plus/dist/index.css";
import {
  renderWithQiankun,
  qiankunWindow,
} from "vite-plugin-qiankun/dist/helper";
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';

// let instance = null;

const render = (props = {}) => {
  const instance = createApp(App);
  Object.keys(ElIcon).forEach((key) => {
    instance.component(key, ElIcon[key]);
  });
  instance.config.globalProperties.$menus = props.menus;
  console.log(props.menus);
  let routes = router.getRoutes();
  let routeToRemove = true;
  routes.forEach((route) => {
    routeToRemove = true;
    for (let i = 0; i < props.menus.length; i += 1) {
      if (props.menus[i].startsWith(route.path)) {
        routeToRemove = false;
        break;
      }
    }
    if (routeToRemove) {
      router.removeRoute(route.name);
    }
  });
  // console.log(router.getRoutes())
  const pinia = createPinia();
  instance
    .use(router)
    .use(pinia)
    .mount(props.container ? props.container.querySelector("#app") : "#app");
};

if (qiankunWindow.__POWERED_BY_QIANKUN__) {
  renderWithQiankun({
    bootstrap: () => console.log("子应用初始化"),
    mount: (props) => {
      console.log("子应用挂载", props);
      render(props);
    },
    unmount: (props) => {
      console.log("子应用卸载");
      instance.unmount();
    },
  });
} else {
  render();
}
